package com.tiancheng.trade.payment.channel.umsshop.util;

import com.alibaba.fastjson2.JSONObject;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.commom.core.utils.ForestHttpUtil;
import com.tiancheng.trade.payment.channel.umsmop.constants.ConfigBean;
import com.tiancheng.trade.payment.channel.umsmop.util.OpenApiCache;
import com.tiancheng.trade.payment.channel.umsmop.util.OpenApiContext;
import com.tiancheng.trade.payment.channel.umsmop.util.OpenBodySigUtil;
import com.tiancheng.trade.payment.channel.umsshop.UmsShopProperties;
import com.tiancheng.trade.payment.channel.umsshop.response.UmpShopApiResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.UUID;

/**
 * @Author: kellen
 * @Description: 银商-新购物车http工具
 * @Date: create in 2025/03/04 19:42
 */
@Slf4j
@Data
public class UmsShopHttpUtils<T> {

    /**
     * 开放平台URL
     */
    private String serverUrl;
    /**
     * 接口地址
     */
    private String interfaceUrl;
    /**
     * appId
     */
    private String appId;
    /**
     * appKey
     */
    private String appKey;

    public UmsShopHttpUtils(UmsShopProperties umsShopProperties, String interfaceUrl){
        this.serverUrl=umsShopProperties.getApiUrl();
        this.interfaceUrl=interfaceUrl;
        this.appId=umsShopProperties.getAppId();
        this.appKey=umsShopProperties.getAppKey();
    }

    /**
     * http post signature方式
     *
     * @param resquest_
     * @return
     * @throws Throwable
     */
    public <T extends UmpShopApiResponse> T doPostSignature(String resquest_, Class<T> t) throws Exception {
        String url = this.serverUrl=this.interfaceUrl;
        OpenApiContext context = new OpenApiContext();
        context.setStartTime(System.currentTimeMillis());
        context.setRequestId(UUID.randomUUID().toString().replace("-",""));
        context.setOpenServUrl(this.serverUrl);
        context.setApiServiceUrl(url);
        context.setAppId(appId);
        context.setAppKey(appKey);
        context.setConfigBean(new ConfigBean());

        context.setRequest(resquest_);

        String authoriztion = OpenBodySigUtil.generateSignature(context, resquest_, "POST");
        Header[] headers = new BasicHeader[]{new BasicHeader("Authorization", authoriztion)};
        final String response = ForestHttpUtil.getInstance().doPostJson(url, context,headers);
        return JSONObject.parseObject(response,t);
    }

    /**
     * http post signature方式
     *
     * @param resquest_
     * @return
     * @throws Throwable
     */
    public <T extends UmpShopApiResponse> T doPostToken(String resquest_,Class<T> t) throws Exception {
        String url = this.serverUrl+this.interfaceUrl;
        OpenApiContext context = new OpenApiContext();
        context.setStartTime(System.currentTimeMillis());
        context.setRequestId(UUID.randomUUID().toString().replace("-",""));
        context.setOpenServUrl(this.serverUrl);
        context.setApiServiceUrl(url);
        context.setAppId(appId);
        context.setAppKey(appKey);
        context.setConfigBean(new ConfigBean());

        context.setRequest(resquest_);
        OpenApiCache.getCurrentToken(context);
        String authoriztion = OpenBodySigUtil.generateToken(context, resquest_, "POST");
        Header[] headers = new BasicHeader[]{new BasicHeader("Authorization", authoriztion),new BasicHeader("content-type", "application/json")};
        final String response = ForestHttpUtil.getInstance().doPostJson(url, resquest_,headers);
        return JsonUtil.fromJson(response,t);
    }

    public String doGet(String request) {
        OpenApiContext context = new OpenApiContext();
        context.setStartTime(System.currentTimeMillis());
        context.setRequestId(UUID.randomUUID().toString().replace("-",""));
        context.setOpenServUrl(this.serverUrl);
        context.setAppId(appId);
        context.setAppKey(appKey);
        context.setConfigBean(new ConfigBean());

        String url = context.getApiServiceUrl();
        String authoriztion = OpenBodySigUtil.generateSignature(context, request, "GET");
        log.info("拼接的url :{}?{}", url, authoriztion);
        return url + "?" + authoriztion;
    }
}
